import {appRouter, systemRouter, cmsRouter} from '@/router/router.js'

const app = {
  state: {
    cachePage: [],
    //  顶部导航菜单
    topMenuList: [],
    //  左侧导航菜单
    leftMenuList: [],
    //  要展开的菜单数组
    openedSubmenuArr: [],
    routers: [
      ...appRouter
    ],
    position: ''
  },
  mutations: {
    updateTopMenuList (state) {
      let list = []
      appRouter.children.forEach((item, index) => {
        if (item.name !== 'other-router')  {
          list[index] = {
            name: item.name,
            title: item.meta.title,
            icon: item.icon
          }
        }
      })
      state.topMenuList = list
    },
    updateLeftMenuList (state, moduleName) {
      let list = []
      let routerList = {}
      switch (moduleName) {
        case 'system':
          routerList = systemRouter
          break
        case 'cms':
          routerList = cmsRouter
          break
      }
      routerList.children.forEach((item, index) => {
        let childList = []
        if (item.children) {
          item.children.forEach((child, ins) => {
            childList[ins] = {
              name: child.name,
              title: child.meta.title,
              icon: child.icon,
              children: []
            }
          })
        }

        list[index] = {
          name: item.name,
          title: item.meta.title,
          icon: item.icon,
          children: childList
        }
      })

      state.leftMenuList = list
    },
    setAvatar (state, path) {
      localStorage.avatorImgPath = path
    },

    savePosition (state, position) {
      state.position = position
    }
  }
}

export default app
